#define _CRT_SECURE_NO_WARNINGS 1

#include"Slist.h"

void SLPushFront(SL** pphead, DataType x)
{
	assert(pphead);
	SL* newnode = BuyLTNode();
	newnode->data = x;
	newnode->next = *pphead;
	*pphead = newnode;
}

void SLPrint(SL* phead)
{
	SL* tmp = phead;
	while (tmp != NULL) {
		printf("%d->", tmp->data);
		tmp = tmp->next;
	}
	printf("NULL");
}

SL* BuyLTNode()
{
	SL* newnode = (SL*)malloc(sizeof(SL));
	if (newnode == NULL) {
		perror("BuyLTNode::malloc");
	}
	return newnode;
}

void SLProFont(SL** pphead)
{
	assert(pphead);
	assert(*pphead);
	SL* cur = *pphead;
	*pphead = cur->next;
	free(cur);
}

void SLPushBack(SL** pphead, DataType x)
{
	assert(pphead);
	SL* newnode = BuyLTNode();
	newnode->data = x;
	newnode->next = NULL;
	SL* tmp = *pphead;
	if (tmp == NULL) {
		*pphead = newnode;
	}
	else {
		while (tmp->next != NULL) {
			tmp = tmp->next;
		}
		tmp->next = newnode;
	}
}

void SLProBack(SL** pphead)
{
	assert(pphead);
	SL* tmp = *pphead;
	if (tmp->next == NULL) {
		free(tmp);
		*pphead = NULL;
	}

	else {
		while (tmp->next->next != NULL) {
			tmp = tmp->next;
		}
		free(tmp->next);
		tmp->next = NULL;
	}
}

void SLInsertAfter(DataType x, SL* pos)
{
	assert(pos);
	SL* newnode = (SL*)malloc(sizeof(SL));
	newnode->data = x;
	SL* tmp = pos->next;
	pos->next = newnode;
	newnode->next = tmp;
}


SL* SLFind(SL* phead, DataType pos)
{
	SL* tmp = phead;
	while (tmp != NULL) {
		if (pos == tmp->data) {
			return tmp;
		}
		tmp = tmp->next;
	}
	assert(tmp);
}

void SLModify(SL* pos, DataType x)
{
	assert(pos);
	pos->data = x;
}

void SLEraseAfter(SL* pos)
{
	assert(pos);
	assert(pos->next);
	SL* tmp = pos->next;
	pos->next = tmp->next;
	free(tmp);
}

void SListDestroy(SL** plist)
{
	assert(plist);
	SL* tmp = *plist;
	SL* cur = NULL;
	while (tmp != NULL) {
		cur = tmp->next;
		free(tmp);
		tmp = cur;
	}
	*plist = NULL;
}